<?php

/*
 * Funkce pro automatické generování odkazu pro řazení
 * $par - parametr pro řazení
 * $ret - návratová stránka
 * @ výstup - HTML kód s odkazy pro řazení
*/
function mailcontent_sort_arow($par, $ret) {
    return "<a href='".$ret."x".$par."'>▼</a>"."&nbsp"."<a href='".$ret.$par."'>▲</a>";
}  //OK


/*************************************** LISTING SETTINGS *********************************************************/
/*
 * Funkce pro výpis mailových schránek
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_accounts() {
    global $user;
    if (user_access('settings accounts')) {

        $list = t('You have not any accounts for WindowsLive mailbox');
        if (db_result(db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid))) {
            $user_login = db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid);
            $list = "<ul>";
            while ($row = db_fetch_array($user_login)) {
                /*
            if (mailcontent_test_connection($row["username"])){
                $con = t('<span style="color:#44FF00"> - Connection OK..</span>');
            } else {
                $con = t('<span style="color:#FF003C"> - Connection could not be established!!</span>');
            }
                */
                if ($row['server'] != '') {
                    $serverArray = explode(".", $row['server']);
                    $serverString = " (".$serverArray[1].")";
                } else {
                    $serverString = " (".t('default server').")";
                }
                $list = $list.'<li><b>'.$row["username"]."</b>".$serverString.' - <a href="?q=mailcontent/editlogin/'.$row["username"]."/".$row["idacc"].'">edit</a> - <a href="?q=mailcontent/dellogin/'.$row["username"]."/".$row["idacc"].'">delete</a> - <a href="?q=mailcontent/synchro/'.$row['idacc'].'">'.t("synchronize").'</a></li>';
            }
            $list = $list."</ul>";
        }


    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
        $list = "<hr><br><br>".t("You have not permission for this action!");
    }
    return $list;


} //OK
/*
 * Funkce pro výpis akcí
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_actions() {
    global $user;
    if (user_access('settings actions')) {
        $list = t('You have not any actions in module');
        if (db_result(db_query("SELECT * FROM {mailcontent_actions}"))) {
            $result = db_query("SELECT * FROM {mailcontent_actions} ORDER BY name");
            $list = "<ul>";
            while ($row = db_fetch_array($result)) {
                /*
            if (mailcontent_test_connection($row["username"])){
                $con = t('<span style="color:#44FF00"> - Connection OK..</span>');
            } else {
                $con = t('<span style="color:#FF003C"> - Connection could not be established!!</span>');
            }
                */
                $list = $list.'<li><b>'.$row["name"].'</b> - '.$row['description'].' - <a href="?q=mailcontent/editaction/'.$row["ida"].'">edit</a> - <a href="?q=mailcontent/delaction/'.$row["ida"].'">delete</a></li>';
            }
            $list = $list."</ul>";
        }
    } else {
        $list = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $list;
} //OK
/*
 * Funkce pro výpis klíčových slov
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_keywords() {
    global $user;
    if (user_access('settings actions')) {
        $list = t('You have not any keywords in module');
        if (db_result(db_query("SELECT * FROM {mailcontent_keywords}"))) {
            $result = db_query("SELECT * FROM {mailcontent_keywords} ORDER BY keyword");
            $list = "<ul>";
            while ($row = db_fetch_array($result)) {
                /*
            if (mailcontent_test_connection($row["username"])){
                $con = t('<span style="color:#44FF00"> - Connection OK..</span>');
            } else {
                $con = t('<span style="color:#FF003C"> - Connection could not be established!!</span>');
            }
                */
                $list = $list.'<li><b>'.$row["keyword"].'</b> (<i>'.$row["class"].') </i> - '.$row['description'].' - <a href="?q=mailcontent/editkeyword/'.$row["idw"].'">edit</a> - <a href="?q=mailcontent/delkeyword/'.$row["idw"].'">delete</a></li>';
            }
            $list = $list."</ul>";
        }
    } else {
        $list = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $list;
} //OK
/*
 * Funkce pro výpis rolí
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_roles() {
    global $user;
    if (user_access('settings roles')) {
        $list = t('You have not any role in module');
        if (db_result(db_query("SELECT * FROM {mailcontent_role}"))) {
            $result = db_query("SELECT * FROM {mailcontent_role}");
            $list = "<ul>";
            while ($row = db_fetch_array($result)) {
                /*
            if (mailcontent_test_connection($row["username"])){
                $con = t('<span style="color:#44FF00"> - Connection OK..</span>');
            } else {
                $con = t('<span style="color:#FF003C"> - Connection could not be established!!</span>');
            }
                */
                $list = $list.'<li><b>'.$row["name"].'</b> - '.$row['description'].' - <a href="?q=mailcontent/editrole/'.$row["idr"].'">edit</a> - <a href="?q=mailcontent/delrole/'.$row["idr"].'">delete</a></li>';
            }
            $list = $list."</ul>";
        }
    } else {
        $list = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $list;
} //OK
/*
 * Funkce pro výpis servisní tabulky
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_others() {
    global $user;
    if (user_access('settings actions')) {
        $list = t('You have not any advanced settings in module');
        if (db_result(db_query("SELECT * FROM {mailcontent_servis}"))) {
            $result = db_query("SELECT * FROM {mailcontent_servis}");
            $list = "<ul>";
            while ($row = db_fetch_array($result)) {
                /*
            if (mailcontent_test_connection($row["username"])){
                $con = t('<span style="color:#44FF00"> - Connection OK..</span>');
            } else {
                $con = t('<span style="color:#FF003C"> - Connection could not be established!!</span>');
            }
                */
                $list = $list.'<li><b>'.$row["name"].'</b> ('.$row['value'].') - '.$row['description'].' - <a href="?q=mailcontent/editother/'.$row["id"].'">edit</a> - <a href="?q=mailcontent/delother/'.$row["id"].'">delete</a></li>';
            }
            $list = $list."</ul>";
        }
    } else {
        $list = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $list;
} //OK
/*
 * Funkce pro výpis povolených mailu 
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_allowed_mailaddress() {
    global $user;
    if (user_access('settings accounts')) {
        $list = t('You have not any allowed mail address for adding content');
        if (db_result(db_query("SELECT * FROM {mailcontent_subscribers} WHERE duid=".$user->uid))) {
            $allowed_mails = db_query("SELECT * FROM {mailcontent_subscribers} as a join {mailcontent_role} as b on a.idr=b.idr WHERE duid=".$user->uid." ORDER BY mailaddress");
            $list = "<ul>";
            while ($row = db_fetch_array($allowed_mails)) {
                if ($row['idacc'] == 'all') {
                    $accounts_text = t('accounts');
                } else {
                    $accounts_text = t('account');
                }
                $list = $list.'<li><u>'.$row["mailaddress"].'</u> '.t("is assigned for").' <b>'. mailcontent_get_username($row["idacc"]).'</b> '.' '.$accounts_text.' '.t("as").' <b>'.$row['name'].'</b>';
                $list = $list.' - <a href="?q=mailcontent/editmailaddress/'.$row["mailaddress"].'/'.$row["idacc"].'/'.$row["idr"].'">edit</a> - <a href="?q=mailcontent/delmailaddress/'.$row["mailaddress"].'/'.$row["idacc"].'/'.$row["idr"].'">delete</a></li>';
            }
            $list = $list."</ul>";
        }
    } else {
        $list = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $list;
}  //OK
/*************************************** LISTING SETTINGS *********************************************************/

/*
 * Funkce vyhledává v databázi uživatelské jméno k danému mailu
 * $idacc - jedinečné číslo účtu
 * @vrací all, pokud je s tímto parametrem volána nebo nalzené jméno
*/
function mailcontent_get_username($idacc = 0) {
    if ($idacc != all) {
        if (db_result(db_query("SELECT username FROM {mailcontent_login} WHERE idacc=".$idacc))) {
            $result = db_query("SELECT username FROM {mailcontent_login} WHERE idacc=".$idacc);
            while($row = db_fetch_array($result)) {
                return $row['username'];
            }
        }
    } else {
        return "all";
    }
} //OK

/*
 * Funkce pro výpis všech příchozích mailu
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_mail_all($order = 0, $sort = 0, $start = 0) {

    global $user;
    if (user_access('content user')) {

        $ret = "?q=mailcontent/content/content/";

        $result = db_query("SELECT count(idm) FROM {mailcontent_mail}");
        $query = "";
        $link_np = "";
        while ($row = db_fetch_array($result)) {
            foreach ($row as $val) {
                if ($val > 100) {
                    if (!$start) {
                        $start = 0;
                    }
                    $query = "LIMIT ".$start.", 100";
                    if ($start > 0) {
                        $link_np = "<a href='?q=mailcontent/content///".($start-100)."'>".t("<< prev")."</a>";
                    }
                    if ($start+100 < $val) {
                        $link_np = $link_np."<a href='?q=mailcontent/content///".($start+100)."'>".t(" next >>")."</a>";
                    }
                }
            }
        }

        if ($order == 0 || $sort == 0) {
            $order = mailcontent_load_filter('content', 'order');
            $sort = mailcontent_load_filter('content', 'sort');
        }
        $table_header = array("#".mailcontent_sort_arow('idm', $ret), t('Title'), t('From').mailcontent_sort_arow('from_address', $ret), t('Type').mailcontent_sort_arow('type_content', $ret), t('Received').mailcontent_sort_arow('received', $ret), t('Owner').mailcontent_sort_arow('duid', $ret), t('Arcticle #'), t('View'), t('Delete'));
        $table_body = array(array());

        if (db_result(db_query('SELECT * FROM {mailcontent_mail} ORDER BY '.$order.' '.$sort." ".$query))) {
            $all_mails = db_query('SELECT * FROM {mailcontent_mail} ORDER BY '.$order.' '.$sort." ".$query);
        } else {
            return "<br><br><br>".t('You have not any mails in database');
        }

        $i = 0;

        while ($row = db_fetch_array($all_mails)) {
            $name = $row['duid'];
            if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$row['duid']))) {
                $rs = db_query('SELECT name FROM {users} WHERE uid='.$row['duid']);
                $rw = db_fetch_array($rs);
                $name = $rw['name'];
            }
            if ($row["idn"] != 0) {
                $idn = '<a href="?q=mailcontent/gotonode/'.$row['idn'].'">'.$row['idn'].'</a> | <a href="?q=mailcontent/content/content/delnode/'.$row['idm'].'">'.t("unpublish").'</a>';
            } else {
                $idn = t('Not set')." | ".'<a href="?q=mailcontent/content/content/publish/'.$row['idm'].'">'.t("publish").'</a>';
            }
            if ($row['dateexp'] != 0) {
                $expire = date("d.m.Y", strtotime($row['dateexp']));
            } else {
                $expire = t('Not set');
            }

            $table_body[$i] = array($row['idm'], mailcontent_get_title($row['idm']), $row['from_address'], $row['type_content'], date("d.m.Y", strtotime($row['received'])), $name, $idn, '<a href="?q=mailcontent/view/'.$row["idm"].'">'.t("view").'</a>','<a href="?q=mailcontent/delmail/'.$row['idm'].'">'.t("delete!").'</a>');
            /*
        $page = $page."<tr><td></td><td>".."</td><td>".."</td><td>".."</td><td>".."</td><td>"..
        "</td><td>".."</a></td><td><a href='delmail/".."'>".t('delete')."</a></td></tr>"; //TODO dodelat callbacky pro edit a delete
            */
            $i++;

        }
        $page = $page.theme_table($table_header, $table_body);
        $page = $page."<br><br>".$link_np."<br><br>";
    } else {
        $page = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $page;
} //OK

/*
 * Funkce pro výpis všech příchozích mailu typu task
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_task_all($order = 0, $sort = 0, $start = 0) {

    global $user;
    if (user_access('content user')) {

        $result = db_query("SELECT count(idm) FROM {mailcontent_mail} WHERE type_content='task'");
        $query = "";
        $link_np = "";
        while ($row = db_fetch_array($result)) {
            foreach ($row as $val) {
                if ($val > 100) {
                    if (!$start) {
                        $start = 0;
                    }
                    $query = "LIMIT ".$start.", 100";
                    if ($start > 0) {
                        $link_np = "<a href='?q=mailcontent/content//task/".($start-100)."'>".t("<< prev")."</a>";
                    }
                    if ($start+100 < $val) {
                        $link_np = $link_np."<a href='?q=mailcontent/content//task/".($start+100)."'>".t(" next >>")."</a>";
                    }
                }
            }
        }


        if ($order == 0 || $sort == 0) {
            $order = mailcontent_load_filter('task', 'order');
            $sort = mailcontent_load_filter('task', 'sort');
        }
        $table_header = array("#".mailcontent_sort_arow('idm', $ret), t('Title'), t('From').mailcontent_sort_arow('from_address', $ret), t('Received').mailcontent_sort_arow('received', $ret), t('Expire').mailcontent_sort_arow('dateexp', $ret), t('Owner').mailcontent_sort_arow('duid', $ret), t('Arcticle #'), t('Coworkers'), t('Work on it!'), t('View'), t('Delete'));
        $table_body = array(array());

        if (db_result(db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="task" ORDER BY '.$order.' '.$sort.' '.$query))) {
            $all_mails = db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="task" ORDER BY '.$order.' '.$sort.' '.$query);
        } else {
            return "<br><br><br>".t('You have not any tasks in database');
        }

        $i = 0;

        while ($row = db_fetch_array($all_mails)) {
            $name = $row['duid'];
            if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$row['duid']))) {
                $rs = db_query('SELECT name FROM {users} WHERE uid='.$row['duid']);
                $rw = db_fetch_array($rs);
                $name = $rw['name'];
            }
            $subscribers = t("No one");
            if (db_result(db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$row['idm']))) {
                $subscribers = '';
                $res = db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$row['idm']);
                while ($rw = db_fetch_array($res)) {
                    if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$rw['duid']))) {
                        $rs = db_query('SELECT name FROM {users} WHERE uid='.$rw['duid']);
                        $rw1 = db_fetch_array($rs);
                        $subscribers = $subscribers.$rw1['name'].", ";
                    } else {
                        $subscribers = $subscribers.$rw['duid'].", ";
                    }
                }
                $subscribers = substr($subscribers, 0, strlen($subscribers)-2);
            }

            if ($row["idn"] != 0) {
                $idn = '<a href="?q=mailcontent/content/task/gotonode/'.$row['idn'].'">'.$row['idn'].'</a> | <a href="?q=mailcontent/content/task/delnode/'.$row['idm'].'">'.t("unpublish").'</a>';
            } else {
                $idn = t('Not set')." | ".'<a href="?q=mailcontent/content/task/publish/'.$row['idm'].'">'.t("publish").'</a>';
            }
            if ($row['dateexp'] != 0) {
                $expire = date("d.m.Y", strtotime($row['dateexp']));
            } else {
                $expire = t('Not set');
            }
            if (db_result(db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$row['idm']." AND duid=".$user->uid))) {
                $work_on = '<a href="?q=mailcontent/content/task/'.$row['idm'].'">'.t('Stop').'</a>';
            } else {
                $work_on = '<a href="?q=mailcontent/content/task/'.$row['idm'].'">'.t('Start').'</a>';
            }
            $view = '<a href="?q=mailcontent/content/task/X'.$row['idm'].'">'.t("view").'</a>';
            $table_body[$i] = array($row['idm'], mailcontent_get_title($row['idm']), $row['from_address'], date("d.m.Y", strtotime($row['received'])), $expire, $name,  $idn,  $subscribers, $work_on, $view, '<a href="?q=mailcontent/content/task/delmail/'.$row['idm'].'">'.t("delete!").'</a>');
            $i++;
        }
        $page = $page.theme_table($table_header, $table_body);
        $page = $page."<br><br>".$link_np."<br><br>";
    } else {
        $page = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $page;
} //OK

/*
 * Funkce pro výpis všech příchozích mailu typu inquiry
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_inquiry_all($order = 0, $sort = 0, $start = 0) {

    global $user;
    if (user_access('content user')) {



        $result = db_query("SELECT count(idm) FROM {mailcontent_mail} WHERE type_content='inquiry'");
        $query = "";
        $link_np = "";
        while ($row = db_fetch_array($result)) {
            foreach ($row as $val) {
                if ($val > 100) {
                    if (!$start) {
                        $start = 0;
                    }
                    $query = "LIMIT ".$start.", 100";
                    if ($start > 0) {
                        $link_np = "<a href='?q=mailcontent/content//inquiry/".($start-100)."'>".t("<< prev")."</a>";
                    }
                    if ($start+100 < $val) {
                        $link_np = $link_np."<a href='?q=mailcontent/content//inquiry/".($start+100)."'>".t(" next >>")."</a>";
                    }
                }
            }
        }


        if ($order == 0 || $sort == 0) {
            $order = mailcontent_load_filter('inquiry', 'order');
            $sort = mailcontent_load_filter('inquiry', 'sort');
        }
        $ret = "?q=mailcontent/content/inquiry/";
        $table_header = array("#".mailcontent_sort_arow('idm', $ret), t('Title'), t('From').mailcontent_sort_arow('from_address', $ret), t('Received').mailcontent_sort_arow('received', $ret), t('Owner').mailcontent_sort_arow('duid', $ret), t('Arcticle #'), t('Vote!'), t('View'), t('Delete'));
        $table_body = array(array());

        if (db_result(db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="inquiry" ORDER BY '.$order.' '.$sort.' '.$query))) {
            $all_mails = db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="inquiry" ORDER BY '.$order.' '.$sort.' '.$query);
        } else {
            return "<br><br><br>".t('You have not any inquiries in database');
        }

        $i = 0;

        while ($row = db_fetch_array($all_mails)) {
            $name = $row['duid'];
            if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$row['duid']))) {
                $rs = db_query('SELECT name FROM {users} WHERE uid='.$row['duid']);
                $rw = db_fetch_array($rs);
                $name = $rw['name'];
            }
            if ($row["idn"] != 0) {
                $idn = '<a href="?q=mailcontent/content/inquiry/gotonode/'.$row['idn'].'">'.$row['idn'].'</a> | <a href="?q=mailcontent/content/inquiry/delnode/'.$row['idm'].'">'.t("unpublish").'</a>';
            } else {
                $idn = t('Not set')." | ".'<a href="?q=mailcontent/content/inquiry/publish/'.$row['idm'].'">'.t("publish").'</a>';
            }
            if ($row['dateexp'] != 0) {
                $expire = date("d.m.Y - H:m:s", strtotime($row['dateexp']));
            } else {
                $expire = t('Not set');
            }
            if ($row['datestart'] != 0) {
                $start = date("d.m.Y - H:m:s", strtotime($row['datestart']));
            } else {
                $start = t('Not set');
            }
            global $user;
            if (db_result(db_query("SELECT * FROM {mailcontent_inquries_votes} WHERE vote_idm=".$row['idm']." AND vote_duid=".$user->uid))) {
                $vote = t("voted").' | <a href="?q=mailcontent/content/inquiry/'.$row['idm'].'">'.t("change vote").'</a>';
            } else {
                $vote = t("not voted").' | <a href="?q=mailcontent/content/inquiry/'.$row['idm'].'">'.t("vote for..").'</a>';
            }


            $view = '<a href="?q=mailcontent/content/inquiry/X'.$row['idm'].'">'.t("view").'</a>';
// TODO udelat vyhodnoceni anket
            $table_body[$i] = array($row['idm'], mailcontent_get_title($row['idm']), $row['from_address'], date("d.m.Y", strtotime($row['received'])), $name,  $idn, $vote, $view, '<a href="?q=mailcontent/content/inquiry/delmail/'.$row['idm'].'">'.t("delete!").'</a>');
            $i++;
        }
        $page = $page.theme_table($table_header, $table_body);
        $page = $page."<br><br>".$link_np."<br><br>";
    } else {
        $page = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $page;
} //OK

/*
 * Funkce pro výpis všech příchozích mailu typu schedule
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_schedule_all($order = 0, $sort = 0, $start = 0) {

    global $user;
    if (user_access('content user')) {



        $result = db_query("SELECT count(idm) FROM {mailcontent_mail} WHERE type_content='schedule'");
        $query = "";
        $link_np = "";
        while ($row = db_fetch_array($result)) {
            foreach ($row as $val) {
                if ($val > 100) {
                    if (!$start) {
                        $start = 0;
                    }
                    $query = "LIMIT ".$start.", 100";
                    if ($start > 0) {
                        $link_np = "<a href='?q=mailcontent/content//schedule/".($start-100)."'>".t("<< prev")."</a>";
                    }
                    if ($start+100 < $val) {
                        $link_np = $link_np."<a href='?q=mailcontent/content//schedule/".($start+100)."'>".t(" next >>")."</a>";
                    }
                }
            }
        }

        $ret = "?q=mailcontent/content/schedule/";
        if ($order == 0 || $sort == 0) {
            $order = mailcontent_load_filter('schedule', 'order');
            $sort = mailcontent_load_filter('schedule', 'sort');
        }

        $table_header = array("#".mailcontent_sort_arow('idm', $ret), t('Title'), t('From').mailcontent_sort_arow('from_address', $ret), t('Received').mailcontent_sort_arow('received', $ret), t('Start time').mailcontent_sort_arow('datestart', $ret), t('End time').mailcontent_sort_arow('dateexp', $ret), t('Owner').mailcontent_sort_arow('duid', $ret), t('Arcticle #'), t('Subscribers'), t('Subscribe it!'), t('View'), t('Delete'));
        $table_body = array(array());

        if (db_result(db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="schedule" ORDER BY '.$order.' '.$sort.' '.$query))) {
            $all_mails = db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="schedule" ORDER BY '.$order.' '.$sort.' '.$query);
        } else {
            return "<br><br><br>".t('You have not any schedules in database');
        }

        $i = 0;

        while ($row = db_fetch_array($all_mails)) {
            $name = $row['duid'];
            if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$row['duid']))) {
                $rs = db_query('SELECT name FROM {users} WHERE uid='.$row['duid']);
                $rw = db_fetch_array($rs);
                $name = $rw['name'];
            }
            $subscribers = t("No one");
            if (db_result(db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$row['idm']))) {
                $subscribers = '';
                $res = db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$row['idm']);
                while ($rw = db_fetch_array($res)) {
                    if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$rw['duid']))) {
                        $rs = db_query('SELECT name FROM {users} WHERE uid='.$rw['duid']);
                        $rw1 = db_fetch_array($rs);
                        $subscribers = $subscribers.$rw1['name'].", ";
                    } else {
                        $subscribers = $subscribers.$rw['duid'].", ";
                    }
                }
                $subscribers = substr($subscribers, 0, strlen($subscribers)-2);
            }

            if ($row["idn"] != 0) {
                $idn = '<a href="?q=mailcontent/content/schedule/gotonode/'.$row['idn'].'">'.$row['idn'].'</a> | <a href="?q=mailcontent/content/schedule/delnode/'.$row['idm'].'">'.t("unpublish").'</a>';
            } else {
                $idn = t('Not set')." | ".'<a href="?q=mailcontent/content/schedule/publish/'.$row['idm'].'">'.t("publish").'</a>';
            }
            if ($row['dateexp'] != 0) {
                $expire = date("d.m.Y - H:m:s", strtotime($row['dateexp']));
            } else {
                $expire = t('Not set');
            }
            if ($row['datestart'] != 0) {
                $start = date("d.m.Y - H:m:s", strtotime($row['datestart']));
            } else {
                $start = t('Not set');
            }
            if (db_result(db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$row['idm']." AND duid=".$user->uid))) {
                $work_on = '<a href="?q=mailcontent/content/schedule/'.$row['idm'].'">'.t('Unsubscribe').'</a>';
            } else {
                $work_on = '<a href="?q=mailcontent/content/schedule/'.$row['idm'].'">'.t('Subscribe').'</a>';
            }
            $view = '<a href="?q=mailcontent/content/schedule/X'.$row['idm'].'">'.t("view").'</a>';
            $table_body[$i] = array($row['idm'], mailcontent_get_title($row['idm']), $row['from_address'], date("d.m.Y", strtotime($row['received'])), $start, $expire, $name,  $idn, $subscribers, $work_on, $view, '<a href="?q=mailcontent/content/schedule/delmail/'.$row['idm'].'">'.t("delete!").'</a>');
            $i++;
        }
        $page = $page.theme_table($table_header, $table_body);
        $page = $page."<br><br>".$link_np."<br><br>";
    } else {
        $page = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $page;
} //OK

/*
 * Funkce pro výpis všech příchozích mailu typu article
 * @vrací HTML kód s jednoduchým výpisem
*/
function mailcontent_list_article_all($order = 0, $sort = 0, $start = 0) {

    global $user;
    if (user_access('content user')) {

        $ret = "?q=mailcontent/content/article/";

        $result = db_query("SELECT count(idm) FROM {mailcontent_mail} WHERE type_content='article'");
        $query = "";
        $link_np = "";
        while ($row = db_fetch_array($result)) {
            foreach ($row as $val) {
                if ($val > 100) {
                    if (!$start) {
                        $start = 0;
                    }
                    $query = "LIMIT ".$start.", 100";
                    if ($start > 0) {
                        $link_np = "<a href='?q=mailcontent/content//article/".($start-100)."'>".t("<< prev")."</a>";
                    }
                    if ($start+100 < $val) {
                        $link_np = $link_np."<a href='?q=mailcontent/content//article/".($start+100)."'>".t(" next >>")."</a>";
                    }
                }
            }
        }


        if ($order == 0 || $sort == 0) {
            $order = mailcontent_load_filter('article', 'order');
            $sort = mailcontent_load_filter('article', 'sort');
        }
        $table_header = array("#".mailcontent_sort_arow('idm', $ret), t('Title'), t('From').mailcontent_sort_arow('from_address', $ret), t('Received').mailcontent_sort_arow('received', $ret), t('Owner').mailcontent_sort_arow('duid', $ret), t('Arcticle #'), t('View'), t('Delete'));
        $table_body = array(array());

        if (db_result(db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="article" ORDER BY '.$order.' '.$sort.' '.$query))) {
            $all_mails = db_query('SELECT * FROM {mailcontent_mail} WHERE type_content="article" ORDER BY '.$order.' '.$sort.' '.$query);
        } else {
            return "<br><br><br>".t('You have not any articles in database');
        }

        $i = 0;

        while ($row = db_fetch_array($all_mails)) {
            $name = $row['duid'];
            $ret = "?q=mailcontent/content/article/";
            if (db_result(db_query('SELECT name FROM {users} WHERE uid='.$row['duid']))) {
                $rs = db_query('SELECT name FROM {users} WHERE uid='.$row['duid']);
                $rw = db_fetch_array($rs);
                $name = $rw['name'];
            }

            if ($row["idn"] != 0) {
                $idn = '<a href="?q=mailcontent/content/article/gotonode/'.$row['idn'].'">'.$row['idn'].'</a> | <a href="?q=mailcontent/content/article/delnode/'.$row['idm'].'">'.t("unpublish").'</a>';
            } else {
                $idn = t('Not set')." | ".'<a href="?q=mailcontent/content/article/publish/'.$row['idm'].'">'.t("publish").'</a>';
            }

            if ($row['datestart'] != 0) {
                $publish = date("d.m.Y - H:m:s", strtotime($row['datestart']));
            } else {
                $publish = t('Not set');
            }

            $view = "<a href='?q=mailcontent/content/article/X".$row['idm']."'>".t("view")."</a>";
            $table_body[$i] = array($row['idm'], mailcontent_get_title($row['idm']), $row['from_address'], date("d.m.Y", strtotime($row['received'])), $name,  $idn, $view, '<a href="?q=mailcontent/content/article/delmail/'.$row['idm'].'">'.t("delete!").'</a>');
            $i++;
        }
        $page = $page.theme_table($table_header, $table_body);
        $page = $page."<br><br>".$link_np."<br><br>";
    } else {
        $page = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $page;

} //OK

/*
 * Z databáze vybere všechny mailové účty a vrátí jejich výčet jako asociativní pole
 * @ asociativ array['ID mailové schránky'] = uživatelské jméno
*/
function mailcontent_get_usernames() {

    global $user;
    if (user_access('settings accounts')) {

        $usernames = array();
        $usernames["all"] = "all";
        if (db_result(db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid))) {
            $user_login = db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid);
            while ($row = db_fetch_array($user_login)) {
                $usernames[$row["idacc"]] = $row["username"];
            }
        }
        return $usernames;

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

}  //OK

/*
 * Z databáze vybere všechny role a vrátí jejich výčet jako asociativní pole
 * @ asociativ array['ID role'] = jmeno role - popis
*/
function mailcontent_get_roles() {
    global $user;
    if (user_access('settings roles')) {

        $roles = array();
        $is_result = db_result(db_query("SELECT * FROM {mailcontent_role}"));
        if ($is_result) {
            $user_login = db_query("SELECT * FROM {mailcontent_role");
            while ($row = db_fetch_array($user_login)) {
                $roles[$row["idr"]] = $row["name"]." - ".$row['description'];
            }
        }
        return $roles;
    } else {
        $page = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
}//OK

/*
 * Z databáze vybere všechny akce a vrátí jejich výčet jako asociativní pole
 * @ asociativ array['ID akce'] = jmeno akce- popis
*/
function mailcontent_get_actions() {
    global $user;
    if (user_access('settings actions')) {


        $actions = array();
        if (db_result(db_query("SELECT * FROM {mailcontent_actions}"))) {
            $res = db_query("SELECT * FROM {mailcontent_actions}");
            while ($rw = db_fetch_array($res)) {
                $actions[$rw["ida"]] = $rw["name"].' - '.$rw['description'];
            }
            return $actions;
        }
        return FALSE;

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

}//OK

/*
 * Z databáze vyberetitulek přístulšný pro zadaný mail
 * $idm - unikátní číslo mailu
 * @ string - titulek
*/
function mailcontent_get_title($idm = 0) {

    global $user;
    if (user_access('content user')) {

        if (db_result(db_query('SELECT * FROM {mailcontent_content} WHERE type_content="title" AND idm='.$idm))) {
            $res = db_query('SELECT * FROM {mailcontent_content} WHERE type_content="title" AND idm='.$idm);
            while($rw = db_fetch_array($res)) {
                $title = $rw['value_content'];
            }
        } else {
            $title = '';
        }
    } else {
        $title = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $title;
}//OK

?>
